<?php
/**
 * SimpleRbacNodeModel.php
 * Created by PhpStorm.
 * User: 江小波
 * Date: 2019/06/26
 * Time: 22:34
 */
namespace app\common\auto_curd\php_system\model;

use think\Db;

class SimpleRbacNodeModel
{
    /**
     * ID
     */
    protected $id = '';
    public function getId()
    {
        return $this->id;
    }
    public function setId($id)
    {
        $this->id = $id;
    }
    
    /**
     * 分层
     */
    protected $level = '0';
    public function getLevel()
    {
        return $this->level;
    }
    public function setLevel($level)
    {
        $this->level = $level;
    }
    
    /**
     * 创建时间
     */
    protected $create_ts = '0';
    public function getCreateTs()
    {
        return $this->create_ts;
    }
    public function setCreateTs($create_ts)
    {
        $this->create_ts = $create_ts;
    }
    
    /**
     * 修改时间
     */
    protected $update_ts = '0';
    public function getUpdateTs()
    {
        return $this->update_ts;
    }
    public function setUpdateTs($update_ts)
    {
        $this->update_ts = $update_ts;
    }
    
    /**
     * 上级
     */
    protected $pid = '0';
    public function getPid()
    {
        return $this->pid;
    }
    public function setPid($pid)
    {
        $this->pid = $pid;
    }
    
    /**
     * 顶级
     */
    protected $tid = '0';
    public function getTid()
    {
        return $this->tid;
    }
    public function setTid($tid)
    {
        $this->tid = $tid;
    }
    
    /**
     * 排序
     */
    protected $sort = '100';
    public function getSort()
    {
        return $this->sort;
    }
    public function setSort($sort)
    {
        $this->sort = $sort;
    }
    
    /**
     * 节点名
     */
    protected $name = '';
    public function getName()
    {
        return $this->name;
    }
    public function setName($name)
    {
        $this->name = $name;
    }
    
    /**
     * 节点标题
     */
    protected $title = '';
    public function getTitle()
    {
        return $this->title;
    }
    public function setTitle($title)
    {
        $this->title = $title;
    }
    
    /**
     * 树形描述
     */
    protected $tree = '';
    public function getTree()
    {
        return $this->tree;
    }
    public function setTree($tree)
    {
        $this->tree = $tree;
    }
    
    /**
     * 节点描述
     */
    protected $remark = '';
    public function getRemark()
    {
        return $this->remark;
    }
    public function setRemark($remark)
    {
        $this->remark = $remark;
    }
    
    /**
     * constructor.
     * @param array $data
     */
    public function __construct($data=[])
    {

        // ID
        if(isset($data['id']))
        {
            $this->id = $data['id'];
        }

        // 分层
        if(isset($data['level']))
        {
            $this->level = $data['level'];
        }

        // 创建时间
        if(isset($data['create_ts']))
        {
            $this->create_ts = $data['create_ts'];
        }

        // 修改时间
        if(isset($data['update_ts']))
        {
            $this->update_ts = $data['update_ts'];
        }

        // 上级
        if(isset($data['pid']))
        {
            $this->pid = $data['pid'];
        }

        // 顶级
        if(isset($data['tid']))
        {
            $this->tid = $data['tid'];
        }

        // 排序
        if(isset($data['sort']))
        {
            $this->sort = $data['sort'];
        }

        // 节点名
        if(isset($data['name']))
        {
            $this->name = $data['name'];
        }

        // 节点标题
        if(isset($data['title']))
        {
            $this->title = $data['title'];
        }

        // 树形描述
        if(isset($data['tree']))
        {
            $this->tree = $data['tree'];
        }

        // 节点描述
        if(isset($data['remark']))
        {
            $this->remark = $data['remark'];
        }

    }
    
    /**
     * 获取分页信息
     * @param $where
     * @param array $extend
     * @return \think\Paginator
     */
    static public function paginate($where,$extend=[])
    {
        $handler = Db::table('simple_rbac_node');
        if(!empty($extend['field']))
        {
            $handler->field($extend['field']);
        }
        if(!empty($extend['order']))
        {
            $handler->order($extend['order']);
        }
        if(!empty($extend['group']))
        {
            $handler->group($extend['group']);
        }
        return $handler
            ->where($where)
            ->paginate();
    }
    
    /**
     * 添加|修改
     * @param $flag
     * @param $data
     * @param array $extend
     * @return int|string
     */
    static public function save($flag,$data,$extend=[])
    {
        if(empty($flag))
        {
            // 添加
            $data['create_ts'] = time();
            return Db::table('simple_rbac_node')
                ->insertGetId($data);
        }
        else
        {
            // 编辑
            $data['update_ts'] = time();
            return Db::table('simple_rbac_node')
                ->where('id',$flag)
                ->update($data);
        }
    }
    
    /**
     * 获取详情
     * @param $where
     * @param array $extend
     * @return array|false|null|\PDOStatement|string|\think\Model
     */
    static public function get($where,$extend=[])
    {
        $handler = Db::table('simple_rbac_node');
        if(!empty($extend['field']))
        {
            $handler->field($extend['field']);
        }
        if(!empty($extend['order']))
        {
            $handler->order($extend['order']);
        }
        if(!empty($extend['group']))
        {
            $handler->group($extend['group']);
        }
        return $handler
            ->where($where)
            ->find();
    }
    
    /**
     * 删除
     * @param $flag
     * @param array $extend
     * @return int
     */
    static public function delete($flag,$extend=[])
    {
        return Db::table('simple_rbac_node')
            ->where('id',$flag)
            ->delete();
    }
}